/**
 * @author kzm
 * @version 1.0
 * @license MIT-License (2007)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 
 */
package com.mediacatalyst.ui {
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.Font;
	
	import com.mediacatalyst.lang.IDeconstructable;
	import com.mediacatalyst.lang.IPrintable;	
/**
 * Handels font loading and registration.
 */
	public class FontFactory extends EventDispatcher implements IPrintable, IDeconstructable{
		private var _font:String;
   		private var _loader:Loader;
		/**
		 * If parameters given it will load the font.
		 *
		 * @param name the name of the font contained in the swf
		 * @param request the urlrequest pointing to the swf file
		 */
		public function FontFactory(name:String=null,request:URLRequest=null){
			if(name!=null && request!=null)
				load(name,request);
		}
		/**
		 * @see com.mediacatalyst.lang.IDeconstructable#deconstruct()
		 */
		public function deconstruct():void{
			_deconstructLoader();
		}
		private function _constructLoader():void{
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _completeListener,false,0,true);
			_loader.contentLoaderInfo.addEventListener(Event.CLOSE, _closeListener,false,0,true);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, _errorListener,false,0,true);
		}
		private function _deconstructLoader():void{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, _completeListener,false);
			_loader.contentLoaderInfo.removeEventListener(Event.CLOSE, _closeListener,false);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, _errorListener,false);
			_loader = null;
		}

		private function _completeListener(e:Event):void {
	    	var font:Class = LoaderInfo(e.target).applicationDomain.getDefinition(_font) as Class;
	    	Font.registerFont(font);
     		if(exists(_font))
     			dispatchEvent(new FontFactoryEvent(FontFactoryEvent.DONE));
     		else
     			dispatchEvent(new FontFactoryEvent(FontFactoryEvent.FAILED));
     		_font = null;
     		_deconstructLoader();
		}

		private function _errorListener(e:IOErrorEvent):void {
			dispatchEvent(new FontFactoryEvent(FontFactoryEvent.IOERROR));
			_font = null;
			_deconstructLoader();
		}

		private function _closeListener(e:Event):void {
			dispatchEvent(new FontFactoryEvent(FontFactoryEvent.CANCELED));
			_font = null;
			_deconstructLoader();
		}
		/**
		 * Loads the swf containing the font.
		 */
		public function load(name:String,request:URLRequest):void {
			if(_font != null)
				_loader.close();
			_constructLoader();
			_font = name;
		    var context:LoaderContext = new LoaderContext();
		    context.applicationDomain = ApplicationDomain.currentDomain;
		    _loader.load(request,context);
		}

		public static function exists(name:String):Boolean{
			return getFont(name)!=null;
		}
		
		public static function getFont(name:String):Font{
			var fonts:Array = Font.enumerateFonts();
			for each(var font:Font in fonts){
				if(font.fontName==name)
					return font;	
			}
			return null;
		}
		/**
		 * returns the string representation of the object
		 * @return the string
		 */
		override public function toString():String{
			return "FontFactory(font="+_font+",url="+(_loader.contentLoaderInfo!=null?_loader.contentLoaderInfo.url:"null")+")";
		}	
	}
}